# -*- utf-8 -*-

import requests
import json
import re
from time import sleep
from bs4 import BeautifulSoup
import time
import datetime
from requests.adapters import HTTPAdapter
from time import strftime, localtime


def unicode_to_cn(in_str, debug=False):
    out = None

    if isinstance(in_str, bytes):
        temp = str(in_str, encoding='utf-8')
        out = temp.encode('utf-8').decode('unicode_escape')
    else:
        out = in_str.encode('utf-8').decode('unicode_escape')

    return out



def baojing(wenben):
    dingding = '报警地址'

    headers = {
        'Content-Type': 'application/json'
    }
    aaa = {"msgtype": "text",
            "text": {
                "content": wenben
            }}
    resp = requests.post(url=dingding, headers=headers, json=aaa)
    print(resp)

def get_bs(link):
    headers = {

        'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 '
                      'Safari/537.36 '

    }
    s = requests.Session()
    s.mount('http://', HTTPAdapter(max_retries=3))  # 设置重试次数为3次
    s.mount('https://', HTTPAdapter(max_retries=3))
    response = s.get(link, headers=headers, timeout=150)
    ttt = response.text

    f1 = open('D:/微博监控/黑猫投诉/已报警内容.txt', 'r+', encoding='utf-8')
    list1 = f1.readlines() #readlines获得的直接就是列表了

    if 'item_count":0' in ttt:
        print('没有找到结果')
    else:
        item_count = ttt.split('item_count":')[1].split('}')[0]   #切割出投诉数量
        print('共条%s结果' % item_count)
        print('最近一条投诉内容如下：')

        title = ttt.split('title":"')[1].split('","')[0]   #切割出投诉标题
        title1 = re.sub('<(S*?)[^>]*>.*?|<.*? />', '', title)
        title2 = unicode_to_cn(title1)
        print('标题：' + title2)
        
        summary = ttt.split('summary":"')[1].split('","')[0]   #切割出投诉内容
        summary1 = re.sub('<(S*?)[^>]*>.*?|<.*? />', '', summary)
        summary2 = unicode_to_cn(summary1)
        print('投诉内容：' + summary2)

        cotitle = ttt.split('cotitle":"')[1].split('","')[0]  #投诉对象
        cotitle1 = re.sub('<(S*?)[^>]*>.*?|<.*? />', '', cotitle)
        cotitle2 = unicode_to_cn(cotitle1)
        print('投诉对象：' + cotitle2)
        
        print('https://tousu.sina.com.cn/index/search/?keywords=%E7%9F%B3%E5%8A%9B%E6%B4%BE&t=0')

        summary2_1 = summary2 + '\n' #投诉内容加换行，用来判断是否已报警过
        if summary2_1 in list1:
            print('已经报警过了')
        else:
            summary3 = summary2 + ':' + 'https://tousu.sina.com.cn/index/search/?keywords=%E7%9F%B3%E5%8A%9B%E6%B4%BE&t=0'
            baojing(summary3)
            f1.writelines(summary2_1)
    f1.close()



#f = open('已报警内容.txt', 'w', encoding='utf-8')

while 1:
    url = 'https://tousu.sina.com.cn/api/index/s?callback=jQuery1112004377056553832581_1597024697929&keywords=%E7%9F' \
          '%B3%E5%8A%9B%E6%B4%BE&page_size=10&page=1&_=1597024697930 '
    try:
        page = get_bs(url)
        print('黑猫平台监控完毕...30分钟后进行下一轮扫描'+'\n')
        print(strftime('%Y-%m-%d %H:%M:%S', localtime()))
        time.sleep(1800)
    except Exception as e:
        print(e)
        time.sleep(7200)

    continue


